package gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.GridBagLayout;
import java.awt.FlowLayout;
import javax.swing.JProgressBar;
import javax.swing.SpringLayout;

import logic.operation.IOperationObserver;
import javax.swing.JLabel;
import javax.swing.JButton;

/**
 * Okienko ukazujące postęp operacji. 
 * Jest zamykane automatycznie w momencie zakończenia operacji
 * @author Marcin
 *
 */
@SuppressWarnings("serial")
public class ProgressDialog extends JDialog implements IOperationObserver {

	private JPanel contentPane;
	private JProgressBar progressBar;
	private String operationName;
	private JLabel label;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ProgressDialog frame = new ProgressDialog("Sth Sth Sth Sth");
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public ProgressDialog(String operationName) {
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 300, 122);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		SpringLayout sl_contentPane = new SpringLayout();
		contentPane.setLayout(sl_contentPane);
		
		progressBar = new JProgressBar();
		sl_contentPane.putConstraint(SpringLayout.WEST, progressBar, 0, SpringLayout.WEST, contentPane);
		sl_contentPane.putConstraint(SpringLayout.SOUTH, progressBar, -34, SpringLayout.SOUTH, contentPane);
		sl_contentPane.putConstraint(SpringLayout.EAST, progressBar, -5, SpringLayout.EAST, contentPane);
		System.out.println((this.getSize().height)/2);
		contentPane.add(progressBar);
		
		label = new JLabel("Not implemented yet also");
		sl_contentPane.putConstraint(SpringLayout.WEST, label, 0, SpringLayout.WEST, progressBar);
		sl_contentPane.putConstraint(SpringLayout.SOUTH, label, -2, SpringLayout.NORTH, progressBar);
		contentPane.add(label);
		
		JButton btnCancel = new JButton("Cancel");
		sl_contentPane.putConstraint(SpringLayout.SOUTH, btnCancel, 0, SpringLayout.SOUTH, contentPane);
		sl_contentPane.putConstraint(SpringLayout.EAST, btnCancel, 0, SpringLayout.EAST, progressBar);
		contentPane.add(btnCancel);
		
		System.out.println((this.getSize().height)/2);
		
		this.operationName = operationName;
	}
	
	public JProgressBar getProgressBar() {
		return progressBar;
	}

	
	@Override
	public void finished() {
		this.dispose();
	}

	@Override
	public void update(double percentDone, String fileName) {
		setTitle(operationName + "... " + Integer.toString( (int) (percentDone * 100) ) + "%");
		
		progressBar.setValue( (int) (percentDone * 100) );
		
		label.setText(operationName + " " + fileName);
		
	}
}
